package Group9.TodoManager.Data;

import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;

import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;

/**
 * TaskCellRenderer is the renderer class for pop up list.
 * It renders task objects so that they are displayed in the list as desired
 * @author volkan cambazoglu
 *
 */
public class TaskCellRenderer extends JPanel implements ListCellRenderer {
	
	private JLabel label1;
	private JLabel label2;
	private JLabel label3;	

	/**
	 * Default Constructor. Creates an instance of the cell renderer.
	 * Does basic initialization.
	 */
	public TaskCellRenderer() {
		super();

		setLayout(new GridLayout(1,3));
		label1 = new JLabel();
		label2 = new JLabel();
		label3 = new JLabel();
		label1.setOpaque(true);
		label2.setOpaque(true);
		label3.setOpaque(true);
		add(label1);
		add(label2);
		add(label3);
	}

	/**
	 * Used to get the component to be rendered
	 * @param list the list that the component belongs to
	 * @param arg1 the object to be rendered in a cell
	 * @param index 
	 * @param isSelected Is the component selected?
	 * @param isCellFocused
	 */
	@Override
	public Component getListCellRendererComponent(JList list, Object arg1,
			int index, boolean isSelected, boolean isCellFocused) {
		
		Task task = (Task) arg1;
		
		label1.setText(task.getTitle());
		label2.setText(task.getBeginDate().toString());
		label3.setText(task.getEndDate().toString());
		
		if(isSelected){
			label1.setBackground(Color.BLUE);
			label1.setForeground(Color.WHITE);
			label2.setBackground(Color.BLUE);
			label2.setForeground(Color.WHITE);
			label3.setBackground(Color.BLUE);
			label3.setForeground(Color.WHITE);
		}
		else{
			label1.setBackground(Color.WHITE);
			label1.setForeground(Color.BLACK);
			label2.setBackground(Color.WHITE);
			label2.setForeground(Color.BLACK);
			label3.setBackground(Color.WHITE);
			label3.setForeground(Color.BLACK);
		}
		
		setEnabled(list.isEnabled());
		setFont(list.getFont());
		
		return this;
	}
}
